package com.example.smart_city_0829.activity.lawyer_consultation;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.activity.lawyer_consultation.fragment.CommentListFragment;
import com.example.smart_city_0829.activity.lawyer_consultation.fragment.LawyerInfoFragment;
import com.example.smart_city_0829.adapter.BaseFragmentViewPagerAdapter;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.BaseFragment;
import com.example.smart_city_0829.entity.lawyer_onsultation.LawyerList;
import com.example.smart_city_0829.utils.Constant;
import com.google.android.material.tabs.TabItem;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.reflect.TypeToken;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

public class LawyerDetailActivity extends BaseActivity {

    private Button quesBt;
    private CardView cardView;
    private ImageView userAvatar;
    private TextView nameTv;
    private TextView goodAtTv;
    private TextView quesTimesTv;
    private TextView serTimesTv;
    private TabLayout tab;
    private TabItem serType;
    private TabItem userCom;
    private ViewPager vp;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_lawyer_deatil;
    }

    @Override
    protected void initView() {
        quesBt = (Button) findViewById(R.id.ques_bt);
        cardView = (CardView) findViewById(R.id.cardView);
        userAvatar = (ImageView) findViewById(R.id.user_avatar);
        nameTv = (TextView) findViewById(R.id.name_tv);
        goodAtTv = (TextView) findViewById(R.id.goodAt_tv);
        quesTimesTv = (TextView) findViewById(R.id.quesTimes_tv);
        serTimesTv = (TextView) findViewById(R.id.serTimes_tv);
        tab = (TabLayout) findViewById(R.id.tab);
        serType = (TabItem) findViewById(R.id.ser_type);
        userCom = (TabItem) findViewById(R.id.user_com);
        vp = (ViewPager) findViewById(R.id.vp);
    }

    @Override
    protected void initData() {
        LawyerList.RowsBean lawyerData = (LawyerList.RowsBean) gson.fromJson(
                getIntent().getStringExtra("lawyerData"),
                new TypeToken<LawyerList.RowsBean>(){}.getType()
        );

        if (lawyerData != null) {
            fragments =Arrays.asList(LawyerInfoFragment.newInstance(gson.toJson(lawyerData), null), CommentListFragment.newInstance(gson.toJson(lawyerData), null));

            Glide.with(context)
                    .load(Constant.BASE_URL + lawyerData.getAvatarUrl())
                    .into(userAvatar);

            nameTv.setText(String.valueOf(lawyerData.getName()));
            goodAtTv.setText("法律专长：" + lawyerData.getLegalExpertiseName());
            quesTimesTv.setText("咨询人数：" + lawyerData.getServiceTimes());
//            serTimesTv.setText("服务次数："+);
        }

        vp.setAdapter(new PageAdapter(getSupportFragmentManager(),0));
        tab.setupWithViewPager(vp);
    }

    private List<Fragment> fragments;
    private final String[] tabTitles = new String[]{"服务方式","用户评价"};

    @Override
    protected void initEvent() {
        quesBt.setOnClickListener((v) -> {
            final Intent intent = new Intent(context, ConsultationActivity.class);
            intent.putExtra("lawyerData",getIntent().getStringExtra("lawyerData"));
            startActivity(intent);
        });
    }

    @Override
    protected String getActionBarTitle() {
        return null;
    }

    private class PageAdapter extends FragmentPagerAdapter {

        public PageAdapter(@NonNull FragmentManager fm, int behavior) {
            super(fm, behavior);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return tabTitles[position];
        }
    }
}